package com.ygs.secs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Profession;
import com.ygs.secs.service.ProfessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专业管理模块控制器
 *
 * @author Yunxi
 */
@Controller
@RequestMapping("/admin")
public class ProfessionManagerController {
    @Autowired
    private ProfessionService professionService;

    /**
     * 根据查询所有专业信息分页
     *
     * @return 专业集合
     */
    @RequestMapping("/getAllProfessionPage")
    @ResponseBody
    public Map<String, Object> getAllProfessionPage(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int limit) {
        Map<String, Object> professionData = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(page, limit);
            List<Profession> professionList = professionService.getAllProfession();
            if (professionList.size() > 0) {
                professionData.put("code", 200);
                professionData.put("msg", "专业信息查询成功");
                professionData.put("data", professionList);
                professionData.put("count", startPage.getTotal());
                return professionData;
            } else {
                professionData.put("code", 201);
                professionData.put("msg", "数据为null，请先添加");
                return professionData;
            }
        } catch (Exception e) {
            professionData.put("code", 500);
            professionData.put("msg", "system error");
            return professionData;
        }
    }

    /**
     * 根据查询所有专业信息
     *
     * @return 专业集合
     */
    @RequestMapping("/getAllProfession")
    @ResponseBody
    public Map<String, Object> getAllProfession() {
        Map<String, Object> professionData = new HashMap<>();
        try {
            List<Profession> professionList = professionService.getAllProfession();
            if (professionList.size() > 0) {
                professionData.put("code", 200);
                professionData.put("msg", "专业信息查询成功");
                professionData.put("data", professionList);
                return professionData;
            } else {
                professionData.put("code", 201);
                professionData.put("msg", "数据为null，请先添加");
                return professionData;
            }
        } catch (Exception e) {
            professionData.put("code", 500);
            professionData.put("msg", "system error");
            return professionData;
        }
    }


    /**
     * 删除专业信息
     *
     * @param professionId 专业ID
     * @return 操作结果
     */
    @RequestMapping("/delProfessionById")
    @ResponseBody
    public Map<String, Object> delProfessionById(@RequestParam("professionId") Integer professionId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = professionService.deleteByPrimaryKey(professionId);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "删除专业失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }

    /**
     * 添加专业信息
     *
     * @param profession 专业实体
     * @return 操作结果
     */
    @RequestMapping(value = "/addProfession", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addProfession(Profession profession) {
        Map<String, Object> result = new HashMap<>();
        int res = professionService.insertSelective(profession);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "添加数据成功");
        } else {
            result.put("code", 500);
            result.put("msg", "添加数据失败");
        }
        return result;
    }

    /**
     * 修改专业也信息
     *
     * @param profession 专业实体
     * @return 操作结果
     */
    @RequestMapping(value = "/updateProfession", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateProfession(Profession profession) {
        Map<String, Object> result = new HashMap<>();
        int res = professionService.updateByPrimaryKey(profession);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 批量修改专业信息
     *
     * @param jsonProfession 专业实体json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchUpdateProfession", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpdateProfession(String jsonProfession) {
        Map<String, Object> result = new HashMap<>();
        List<Profession> professions = JSONArray.parseArray(jsonProfession, Profession.class);
        int res = professionService.updateBatchSelective(professions);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 批量添加专业信息
     *
     * @param jsonProfession 专业数组json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchInsertProfession", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchInsertProfession(String jsonProfession) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Profession> professions = JSONArray.parseArray(jsonProfession, Profession.class);
            int res = professionService.batchInsert(professions);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 批量删除专业信息
     *
     * @param professions 专业id数组
     * @return 操作结果
     */
    @RequestMapping(value = "/batchDelProfession", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelProfession(Integer[] professions) {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = professionService.batchDelete(professions);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }
}
